{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ANTsPy and ANTsR inherit the ability to do multi-metric registration.\n",
    "\n",
    "Such registrations assume that the feature images are in the same physical space and at least roughly aligned.\n",
    "\n",
    "One may then use registration to optimize a multiple similarity metric objective function as in:\n",
    "\n",
    "https://doi.org/10.3389/fninf.2014.00044\n",
    "\n",
    "https://www.ncbi.nlm.nih.gov/pubmed/18995188\n",
    "\n",
    "First, import ants, read some images and create some features."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ants\n",
    "image = ants.image_read(ants.get_ants_data('r16'))\n",
    "image2 = ants.image_read(ants.get_ants_data('r27'))\n",
    "g1 = ants.iMath_grad( image )\n",
    "g2 = ants.iMath_grad( image2 )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Perform a baseline registration with a single feature and create a couple of new metrics.  Each metric is defined by a name (\"CC\"), the input fixed (image), input moving (image2), a weight value (e.g. 2) and a sampling parameter ( for CC this defines a radius of 9x9 e.g. 4 extra pixels on all sides of the center pixel.  Five entries are needed in total."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg1 = ants.registration( image, image2, 'SyNOnly' )\n",
    "demonsMetric = ['demons', g1, g2, 1, 1]\n",
    "ccMetric = ['CC', image, image2, 2, 4 ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Append the first metric to the metric list. In actuality this means that reg2 will be driven by both a demons metric and the default metric."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "metrics = list( )\n",
    "metrics.append( demonsMetric )\n",
    "reg2 = ants.registration( image, image2, 'SyNOnly',\n",
    "    multivariate_extras = metrics )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add a third metric and run this new registration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "metrics.append( ccMetric )\n",
    "reg3 = ants.registration( image, image2, 'SyNOnly',\n",
    "    multivariate_extras = metrics )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Quantify the results in terms of mutual information of the registration results using the original image intensity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.5175441503528395\n",
      "-0.7395538667097538\n",
      "-0.7289557066574497\n",
      "-0.7649453900726915\n"
     ]
    }
   ],
   "source": [
    "print( ants.image_mutual_information( image, image2 ) )\n",
    "print( ants.image_mutual_information( image, reg1['warpedmovout'] ) )\n",
    "print( ants.image_mutual_information( image, reg2['warpedmovout'] ) )\n",
    "print( ants.image_mutual_information( image, reg3['warpedmovout'] ) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
